<?php

/**
 * 产品
 */
class ProductController extends Controller
{
	/**
	 * 产品
	 */
	public function actionIndex()
	{
		$this->redirect('/product/list');
	}

	/**
	 *产品列表
     */
	public function actionList(){
		$per=15;

		//$type_id=intval($_GET['type_id']);
		$sub_type_id=intval($_GET['sub_type_id']);
		$key=fzr($_GET['key']);$where='';
		$lang=$this->getLang();
		if($lang=='en'){
			$this->pageTitle='Product list'.$this->public_title['en'];
			if(!empty($key)){ $where.=" and (`product_info`.`name_en` like '%$key%' OR `product_sub_type`.`name_en` like '%$key%' OR `product_type`.`name_en` like '%$key%')"; }
		}else{
			$this->pageTitle='产品列表'.$this->public_title['cn'];
			if(!empty($key)){ $where.=" and (`product_info`.`name_cn` like '%$key%' OR `product_sub_type`.`name_cn` like '%$key%' OR `product_type`.`name_cn` like '%$key%')"; }
		}



		if($sub_type_id>0){$where.=" and `product_sub_type`.`id`='$sub_type_id'";}

		$sql="SELECT count(`product_info`.`id`) as `cnt` FROM `product_info`
 			LEFT JOIN `product_sub_type` ON `product_sub_type`.`id` = `product_info`.`type_id`
 			LEFT JOIN `product_type` ON `product_sub_type`.`parent_type_id`=`product_type`.`id`
 			WHERE 1=1 $where ";
		$model=Yii::app()->db->createCommand($sql)->queryAll();
		$cnt=$model[0]['cnt'];

		$page = new PaginationWeb($cnt,$per);
		$page_list = $page->fpage(array(0,1,2,3,4,5,6,7,8));

		//产品数据
		if($lang=='en'){
			$sql="SELECT
	`product_info`.`id` as `id` ,`product_info`.`type_id` as `sub_type_id` ,
	`product_info`.`name_en` as `name` ,`product_info`.`time` as `time` ,`product_info`.`img_count` as `img_count` ,
	`product_sub_type`.`name_en` as `sub_type_name`,
	`product_sub_type`.`name_en` as `type_name`
FROM `product_info`
LEFT JOIN `product_sub_type` ON `product_sub_type`.`id` = `product_info`.`type_id`
LEFT JOIN `product_type` ON `product_sub_type`.`parent_type_id`=`product_type`.`id`
WHERE 1=1 $where ORDER BY `product_sub_type`.`id` ASC,`product_info`.`id` DESC  $page->limit";
		}else{
			$sql="SELECT
	`product_info`.`id` as `id` ,`product_info`.`type_id` as `sub_type_id` ,
	`product_info`.`name_cn` as `name` ,`product_info`.`time` as `time` ,`product_info`.`img_count` as `img_count` ,
	`product_sub_type`.`name_cn` as `sub_type_name`,
	`product_type`.`name_cn` as `type_name`
FROM `product_info`
LEFT JOIN `product_sub_type` ON `product_sub_type`.`id` = `product_info`.`type_id`
LEFT JOIN `product_type` ON `product_sub_type`.`parent_type_id`=`product_type`.`id`
WHERE 1=1 $where ORDER BY `product_sub_type`.`id` ASC,`product_info`.`id` DESC  $page->limit";

		}
		$info_data=Yii::app()->db->createCommand($sql)->queryAll();

		//导航 类型数据
		if($lang=='en'){
			$sql="
SELECT
	`product_type`.`id` AS `id`,
	`product_type`.`name_en` AS `name`,
	`product_sub_type`.`id` AS `sub_type_id`,
	`product_sub_type`.`name_en` AS `sub_type_name`

FROM
	`product_sub_type`
LEFT JOIN `product_type` ON `product_sub_type`.`parent_type_id` = `product_type`.`id`
 			WHERE 1=1  ORDER BY `product_type`.`id` ASC,`product_sub_type`.`id` ASC  ";
		}else{
			$sql="
SELECT
	`product_type`.`id` AS `id`,
	`product_type`.`name_cn` AS `name`,
	`product_sub_type`.`id` AS `sub_type_id`,
	`product_sub_type`.`name_cn` AS `sub_type_name`

FROM
	`product_sub_type`
LEFT JOIN `product_type` ON `product_sub_type`.`parent_type_id` = `product_type`.`id`
 			WHERE 1=1 ORDER BY `product_type`.`id` ASC,`product_sub_type`.`id` ASC  ";
		}

		$type_data=Yii::app()->db->createCommand($sql)->queryAll();

		$this->render('product/list',array('type_data'=>$type_data,'info_data'=>$info_data,'page_list'=>$page_list));
	}










}